import javax.swing.*;
import java.awt.event.*;

public class Login extends JFrame{

    // Front end

    static LoggedInFrame loggedInFrame = null;

    public static void memberLoggingIn(){
        Login frameTable = new Login();
    }

    JButton blogin = new JButton("Login");
    JButton bCreateAccount = new JButton("Create \nAccount");
    JPanel panel = new JPanel();
    JTextField txuser = new JTextField(15);
    JPasswordField pass = new JPasswordField(15);
    JLabel labelUser = new JLabel( "Username", JLabel.LEFT );
    JLabel labelPassword = new JLabel( "Password", JLabel.LEFT );

    Login(){
        super("Login Authentification");
        setSize(450,175);
        setLocation(500,280);
        panel.setLayout (null);

        txuser.setBounds(100,30,150,20);
        pass.setBounds(100,65,150,20);
        blogin.setBounds(140,100,80,20);
        bCreateAccount.setBounds(265,30,140,55);
        labelUser.setBounds(25,30,100,20);
        labelPassword.setBounds(25,65,100,20);

        panel.add(blogin);
        panel.add(bCreateAccount);
        panel.add(txuser);
        panel.add(pass);
        panel.add( labelUser );
        panel.add( labelPassword );

        getContentPane().add(panel);

        setVisible(true);
        clickLogin();
    }

    public void clickLogin(){
        blogin.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ){
                String userName = txuser.getText();
                String userPassword = pass.getText();
                String dbUserResult = "";
                String dbUserPWResult = "";
                try{
                    dbUserResult = DBConnection.getQuery( "select username from userinfo where username = " + "'" + userName + "'", ImageInJFrame.theConnection );
                    dbUserPWResult = DBConnection.getQuery( "select userpassword from userinfo where userpassword = " + "'" + userPassword + "'", ImageInJFrame.theConnection );
                } catch( Exception e ) {
                }

                if( userName.equals( "" ) == true || userPassword.equals( "" ) == true ) {
                    JOptionPane.showMessageDialog( null,"Please enter Username or Password" );
                    txuser.requestFocus();
                } else if(userName.equals(dbUserResult) && userPassword.equals(dbUserPWResult)) {
                    JOptionPane.showMessageDialog( null,"You are now logged in" );
                    ImageInJFrame.loggedIn = true;
                    ImageInJFrame.loggedInName = dbUserResult;
                    Login.loggedInFrame = new LoggedInFrame( ImageInJFrame.loggedInName );
                    dispose();
                } else if( userName.equals( dbUserResult ) == false || userPassword.equals( dbUserPWResult ) == false ) {
                    JOptionPane.showMessageDialog( null,"Wrong Username or Password" );
                    txuser.requestFocus();
                }
            }
        });

        bCreateAccount.addActionListener( new ActionListener(){
            public void actionPerformed( ActionEvent ae ){
                NewCreateAccountFrame regFace = new NewCreateAccountFrame();
                regFace.setVisible( true );
                dispose();
            }
        });


    }

    // Back end

    private int customerID;
    private boolean isLoggedIn;

    public void doIt(){}

    public boolean checkLoggedIn(){
        return isLoggedIn;
    }
}

